﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GameDataAccess" in code, svc and config file together.
    public class GameDataAccess : IGameDataAccess
    {

        public bool saveAchievement(Achievement achObj)
        {
            try
            {
                using (var db = new questEntities())
                {
                    achObj.achievement_date = DateTime.Now;
                   
                    db.Achievements.AddObject(achObj);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }


        public bool saveStudentProgress(int student_id, int fk_schoolGameId)
        {
            try
            {
                using (var db = new questEntities())
                {
                    Student_Progresses spObj = new Student_Progresses();
                    spObj.fk_schoolGame_id = fk_schoolGameId;
                    spObj.fk_student_id = student_id;
                    spObj.game_completion_status = false; //false means not finished
                    db.Student_Progresses.AddObject(spObj);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public int getStudentProgressByStudentID_GameID(int student_id, int fk_schoolGameId)
        {
            List<Student_Progresses> listss = new List<Student_Progresses>();
             using (var db = new questEntities())
                {
                    var res = from s in db.Student_Progresses where s.fk_student_id == student_id && s.fk_schoolGame_id==fk_schoolGameId select s;
                    listss = res.ToList<Student_Progresses>();
                    return listss.Last().pk_sp_id;                 
                    //return db.Student_Progresses.Where(p => p.fk_student_id == student_id && p.fk_schoolGame_id == fk_schoolGameId).Select(p => p.pk_sp_id);
                }
          
        }

    }
}
